"""
部门表 Schema
"""

from datetime import datetime
from typing import List, Optional

from pydantic import Field, field_serializer, field_validator

from app.schemas.base import BaseSchema


class DeptBase(BaseSchema):
    """部门表 基础模型"""

    name: str = Field(max_length=100, description="部门名称")
    code: str = Field(max_length=100, description="部门编号")
    tree_path: str = Field(max_length=255, description="父节点id路径")
    parent_id: Optional[int] = Field(default=None, description="父节点id")
    sort: Optional[int] = Field(default=None, description="显示顺序")
    status: Optional[int] = Field(default=None, description="状态(1-正常 0-禁用)")


class DeptCreate(DeptBase):
    """创建部门表 模型"""

    pass


class DeptUpdate(DeptBase):
    """更新部门表 模型"""

    name: Optional[str] = Field(default=None, max_length=100, description="部门名称")
    code: Optional[str] = Field(default=None, max_length=100, description="部门编号")
    tree_path: Optional[str] = Field(default=None, max_length=255, description="父节点id路径")
    parent_id: Optional[int] = Field(default=None, description="父节点id")
    sort: Optional[int] = Field(default=None, description="显示顺序")
    status: Optional[int] = Field(default=None, description="状态(1-正常 0-禁用)")


class DeptPublic(DeptBase):
    """部门表 公共模型，用于API响应"""

    id: int = Field(description="主键")
    name: str = Field(max_length=100, description="部门名称")
    code: str = Field(max_length=100, description="部门编号")
    tree_path: str = Field(max_length=255, description="父节点id路径")
    parent_id: Optional[int] = Field(default=None, description="父节点id")
    sort: Optional[int] = Field(default=None, description="显示顺序")
    status: Optional[int] = Field(default=None, description="状态(1-正常 0-禁用)")
    create_by: Optional[int] = Field(default=None, description="创建人ID")
    create_time: Optional[datetime] = Field(default=None, description="创建时间")
    update_by: Optional[int] = Field(default=None, description="修改人ID")
    update_time: Optional[datetime] = Field(default=None, description="更新时间")


class DeptVO(BaseSchema):
    """部门视图对象"""

    id: int = Field(description="部门ID")
    parent_id: Optional[int] = Field(default=None, description="父部门ID")
    name: str = Field(max_length=100, description="部门名称")
    code: str = Field(max_length=100, description="部门编号")
    sort: Optional[int] = Field(default=None, description="排序")
    status: Optional[int] = Field(default=None, description="状态(1-正常 0-禁用)")
    children: Optional[List["DeptVO"]] = Field(default=None, description="子部门")
    create_time: Optional[datetime] = Field(default=None, description="创建时间")
    update_time: Optional[datetime] = Field(default=None, description="更新时间")

    @field_serializer("create_time", "update_time", when_used="json")
    def serialize_dt(self, dt: Optional[datetime], _info):
        if dt:
            return dt.strftime("%Y-%m-%d %H:%M")
        return None


class DeptQuery(BaseSchema):
    """部门查询对象"""

    page_num: int = Field(1, description="页码")
    page_size: int = Field(10, description="每页记录数")
    keywords: Optional[str] = Field(default=None, description="关键字(部门名称)")
    status: Optional[int] = Field(default=None, description="状态(1->正常；0->禁用)")


class DeptForm(BaseSchema):
    """部门表单对象"""

    id: Optional[int] = Field(default=None, description="部门ID")
    name: Optional[str] = Field(default=None, max_length=100, description="部门名称")
    code: Optional[str] = Field(default=None, max_length=100, description="部门编号")
    parent_id: Optional[int] = Field(default=None, description="父部门ID", alias="parentId")
    status: Optional[int] = Field(default=None, description="状态(1-正常 0-禁用)")
    sort: Optional[int] = Field(default=None, description="排序")
    op_id: Optional[int] = Field(default=None, description="操作人ID")

    @field_validator("parent_id")
    def validate_parent_id(cls, value):
        if value is None:
            raise ValueError("父部门ID不能为空")
        return value

    @field_validator("status")
    def validate_status(cls, value):
        if value is None:
            raise ValueError("状态不能为空")
        if value not in [0, 1]:
            raise ValueError("状态值不正确")
        return value
